import dayjs from 'dayjs'
import { isArray } from 'xe-utils'
/**
 * 列表参数过滤
 */
function paramsFilter (data, definition = {}, exclude = []) {
  const filterAnd = []
  const allType = {
    like: [],
    range: [],
    rangeDate: [],
    rangeDay: [],
    inKeys: [],
    notKeys: [],
    gt: [],
    lt: [],
    gteDate: [],
    lteDate: [],
    rangeNum: [],
  }
  // console.log();
  const keys = Object.keys(definition)
  if (keys.length) {
    keys.forEach((name) => {
      allType[name] = [...definition[name]]
    })
  }
  const {
    like,
    range,
    rangeDate,
    rangeDay,
    inKeys,
    notKeys,
    gt,
    lt,
    gteDate,
    lteDate,
    rangeNum,
  } = allType
  // 避免影响查询的数据
  data = JSON.parse(JSON.stringify(data))
  for (const [key, value] of Object.entries(data)) {
    if (!value && value !== 0) continue
    if (isArray(value) && value.length == 0) continue
    if (exclude.includes(key)) continue
    if (like.includes(key)) {
      filterAnd.push(
        key, ['like', `%${value}%`]
      )
    } else if (rangeDate.includes(key)) {
      if (data[key] && data[key].length > 1) {
        try {
          const val1 = +new Date(value[0])
          const val2 = +new Date(value[1])
          if (val1 === val2) {
            // 时间戳一样，则最后一个时间戳+1天的毫秒数
            value[1] = val1 + (86400000 - 1)
          } else {
            value[1] = val2 + (86400000 - 1)
          }
        } catch (e) { }
        filterAnd.push(
          key, [">=", dayjs(value[0]).format('YYYY-MM-DD')], key, ['<=', dayjs(value[1]).format('YYYY-MM-DD')]
        )
      }
    } else if (rangeDay.includes(key)) {
      if (data[key] && data[key].length > 1) {
        filterAnd.push(
          {
            [key]: { gt: value[0] },
          },
          {
            [key]: { lt: value[1] },
          }
        )
      }
    } else if (rangeNum.includes(key)) {
      if (data[key]) {
        try {
          if (value[1]) {
            filterAnd.push(
              {
                [key]: { gt: value[0] },
              },
              {
                [key]: { lt: value[1] },
              }
            )
          }
        } catch (e) { }
      }
    } else if (range.includes(key)) {
      // 单日期选择
      if (data[key]) {
        try {
          const val = +new Date(value)
          const val1 = val / 1000
          // 24*3600
          const val2 = val1 + 24 * 3600 - 1
          // console.log(val1, 'val1'); // 小
          // console.log(val2, 'val2'); // 大
          filterAnd.push(
            {
              [key]: { gt: val1 - 1 },
            },
            {
              [key]: { lt: val2 + 1 },
            }
          )
        } catch (e) { }
      }
    } else if (inKeys.includes(key)) {
      filterAnd.push({
        [key]: {
          in: value,
        },
      })
    } else if (notKeys.includes(key)) {
      filterAnd.push({
        [key]: {
          'not in': value,
        },
      })
    } else if (gt.includes(key)) {
      filterAnd.push({
        [key]: {
          gt: value,
        },
      })
    } else if (lt.includes(key)) {
      filterAnd.push({
        [key]: {
          lt: value,
        },
      })
    } else if (gteDate.includes(key)) {
      filterAnd.push({
        [key]: {
          gte: dayjs(value).unix(),
        },
      })
    } else if (lteDate.includes(key)) {
      filterAnd.push({
        [key]: {
          lte: dayjs(value).unix() + 24 * 60 * 60 - 1,
        },
      })
    } else {
      filterAnd.push(
        key, value
      )
    }
  }
  return filterAnd
}

export { paramsFilter }
